package com.fortune.schedule;

import com.faneui.common.utils.DateFormatUtils;
import com.faneui.common.utils.ListUtils;
import com.fortune.dal.model.Debt;
import com.fortune.dal.model.BillInfo;
import com.fortune.service.BillInfoService;
import com.fortune.service.DebtService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 每隔 15天 生成债务还款记录
 */
@Component
public class DebtBillCreateSchedule {

    protected static final Logger logger = LoggerFactory.getLogger(DebtBillCreateSchedule.class);

    @Value("${spring.profiles.active}")
    private String env;

    @Autowired
    private DebtService debtService;

    @Autowired
    private BillInfoService billInfoService;

    @Scheduled(fixedRate = 1000 * 60 * 60 * 24)
    public void run() {
        if(!"prd".equals(env)) {
            return;
        }
        try {
            String date = DateFormatUtils.yyyyMMdd.format(new Date());
            Debt pd = new Debt();
            List<Debt> debts = debtService.queryDebtList(pd);
            if (ListUtils.isEmpty(debts))
                return;
            for (Debt d : debts) {
                Calendar c = Calendar.getInstance();
                c.setTime(DateFormatUtils.yyyyMMdd.parse(d.getNextPayDate()));
                c.add(Calendar.DATE,-20 );
                if (Integer.parseInt(date) < Integer.parseInt(DateFormatUtils.yyyyMMdd.format(c.getTime())) - 20) {
                    continue;
                }
                BillInfo bill = new BillInfo();
                bill.setDeadlineDate(d.getNextPayDate());
                bill.setType("1");
                bill.setIssuerId(d.getId());
                List<BillInfo> bills = billInfoService.queryBillInfoList(bill);

                if (ListUtils.isEmpty(bills)) {

                    String billDate = DateFormatUtils.yyyyMMdd.format(c.getTime());
                    bill.setBillDate(billDate);
                    bill.setTotalAmount(d.getEveryAmount());
                    bill.setResidueAmount(d.getEveryAmount());
                    bill.setStatus("0");
                    billInfoService.addBillInfo(bill);
                }
                c.setTime(DateFormatUtils.yyyyMMdd.parse(d.getNextPayDate()));
                int field = 0;
                // 1-每月，2-日，3-周，4-季，5-年
                switch (d.getCycleType()) {
                    case "1":
                        field = Calendar.MONTH;
                        break;
                    case "2":
                        field = Calendar.DATE;
                        break;
                    case "3":
                        field = Calendar.WEEK_OF_YEAR;
                        break;
                    case "4"://TODO;
                    default:
                        break;
                }
                c.add(field, d.getCycleNum());
                String nextDate = DateFormatUtils.yyyyMMdd.format(c.getTime()) ;
                d.setNextPayDate(nextDate);
                debtService.modifyDebt(d);
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
